import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UseGuards,
  HttpCode,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AdminGuard } from '../admin/admin.guard';
import { CategoryService } from './category.service';
import {
  CreateCategoryDto,
  UpdateCategoryDto,
  CategoryResponseDto,
} from './category.dto';
import { Song } from '../song/song.entity';

@ApiTags('分类')
@Controller('categories')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @ApiOperation({ summary: '创建新分类' })
  @ApiResponse({ status: 201, type: CategoryResponseDto })
  @ApiResponse({ status: 409, description: '分类名称已存在' })
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  @Post()
  async createCategory(
    @Body() createCategoryDto: CreateCategoryDto,
  ): Promise<CategoryResponseDto> {
    return this.categoryService.createCategory(createCategoryDto);
  }

  @ApiOperation({ summary: '获取所有分类' })
  @ApiResponse({ status: 200, type: [CategoryResponseDto] })
  @Get()
  async getAllCategories(): Promise<CategoryResponseDto[]> {
    return this.categoryService.getAllCategories();
  }

  @ApiOperation({ summary: '获取单个分类' })
  @ApiResponse({ status: 200, type: CategoryResponseDto })
  @ApiResponse({ status: 404, description: '分类不存在' })
  @ApiParam({ name: 'id', description: '分类ID' })
  @Get(':id')
  async getCategoryById(@Param('id') id: number): Promise<CategoryResponseDto> {
    return this.categoryService.getCategoryById(id);
  }

  @ApiOperation({ summary: '更新分类' })
  @ApiResponse({ status: 200, type: CategoryResponseDto })
  @ApiResponse({ status: 404, description: '分类不存在' })
  @ApiResponse({ status: 409, description: '分类名称已存在' })
  @ApiParam({ name: 'id', description: '分类ID' })
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  @Put(':id')
  async updateCategory(
    @Param('id') id: number,
    @Body() updateCategoryDto: UpdateCategoryDto,
  ): Promise<CategoryResponseDto> {
    return this.categoryService.updateCategory(id, updateCategoryDto);
  }

  @ApiOperation({ summary: '删除分类' })
  @ApiResponse({ status: 204, description: '删除成功' })
  @ApiResponse({ status: 404, description: '分类不存在' })
  @ApiParam({ name: 'id', description: '分类ID' })
  @UseGuards(AuthGuard('jwt'), AdminGuard)
  @HttpCode(204)
  @Delete(':id')
  async deleteCategory(@Param('id') id: number): Promise<void> {
    return this.categoryService.deleteCategory(id);
  }

  @ApiOperation({ summary: '获取分类下的所有歌曲' })
  @ApiResponse({ status: 200, type: [Song] })
  @ApiResponse({ status: 404, description: '分类不存在' })
  @ApiParam({ name: 'id', description: '分类ID' })
  @Get(':id/songs')
  async getSongsByCategory(@Param('id') id: number): Promise<Song[]> {
    return this.categoryService.getSongsByCategory(id);
  }

  @ApiOperation({ summary: '将歌曲添加到分类' })
  @ApiResponse({ status: 200, description: '添加成功' })
  @ApiResponse({ status: 404, description: '分类或歌曲不存在' })
  @ApiParam({ name: 'categoryId', description: '分类ID' })
  @ApiParam({ name: 'songId', description: '歌曲ID' })
  @UseGuards(AuthGuard('jwt'))
  @Post(':categoryId/songs/:songId')
  async addSongToCategory(
    @Param('categoryId') categoryId: number,
    @Param('songId') songId: number,
  ): Promise<void> {
    return this.categoryService.addSongToCategory(categoryId, songId);
  }

  @ApiOperation({ summary: '从分类中移除歌曲' })
  @ApiResponse({ status: 204, description: '移除成功' })
  @ApiResponse({ status: 404, description: '分类不存在' })
  @ApiParam({ name: 'categoryId', description: '分类ID' })
  @ApiParam({ name: 'songId', description: '歌曲ID' })
  @UseGuards(AuthGuard('jwt'))
  @HttpCode(204)
  @Delete(':categoryId/songs/:songId')
  async removeSongFromCategory(
    @Param('categoryId') categoryId: number,
    @Param('songId') songId: number,
  ): Promise<void> {
    return this.categoryService.removeSongFromCategory(categoryId, songId);
  }
}
